//
//  ViewController.swift
//  uiKit-demo
//
//  Created by 陈澎 on 2024/12/13.
//

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    let item: [String] = ["常用控件", "present与dismiss跳转", "实现瀑布流", "商品列表"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        let layout = UICollectionViewFlowLayout()
        // 自动调整大小
//        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        layout.scrollDirection = .vertical
        
        layout.itemSize = CGSize(width: 80, height: 40)
        // 设置边距
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
        // 调整列间距
        layout.minimumInteritemSpacing = 10
        // 调整行间距
        layout.minimumLineSpacing = 10
        
        layout.headerReferenceSize = CGSize(width: self.view.frame.size.width, height: 10)
        
        let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.register(NSClassFromString("UICollectionViewCell"), forCellWithReuseIdentifier: "cell")
        
        self.view.addSubview(collectionView)
        
        let tabBar = UITabBar()
        let item1 = UITabBarItem(tabBarSystemItem: .favorites, tag: 1)
        tabBar.items = [
            item1
        ]
        view.addSubview(tabBar)
    }
    // 分区个数
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    // 返回每个分区的item个数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return item.count
    }
    // 动态计算cell宽度
    func collectionView(_ collecionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let text = item[indexPath.item]
        let font = UIFont.systemFont(ofSize: 16)
        let textWidth = text.size(withAttributes: [.font: font]).width
        return CGSize(width: textWidth + 40, height: 40) // 动态宽度 + 内边距
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.backgroundColor = .red
        cell.layer.cornerRadius = 10
        // cell.bounds：撑满整个容器
        let label = UILabel(frame: cell.bounds)
        label.text = item[indexPath.item]
        label.textAlignment = .center
        label.textColor = .white
        label.font = UIFont.systemFont(ofSize: 16, weight: .heavy)
        cell.contentView.addSubview(label)
        return cell
    }
    
    // 点击处理
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("第\(indexPath.row)个item被点击")
        if indexPath.row == 0 {
            let demoPage = DemoUIPageControl()
            navigationController?.pushViewController(demoPage, animated: true)
        } else if indexPath.row == 1 {
            let aboutPage = AboutPageControl()
            aboutPage.data = "郑善恩"
            self.present(aboutPage, animated: true)
        } else if indexPath.row == 2 {
            let waterFall = WaterfallViewController()
            navigationController?.pushViewController(waterFall, animated: true)
        } else if indexPath.row == 3 {
            let goodsListPage = GoodsListViewController()
            navigationController?.pushViewController(goodsListPage, animated: true)
        }
    }
}

